version: "3"

services:
  agent:
    image: "{{ wgcloud_docker_imgs.base }}"
    container_name: "wgcloud-agent"
    restart: "always"
    network_mode: "host"
    privileged: "yes"
    volumes:
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime"
      - "{{ wgcloud_deploy_path }}/agent:/opt/agent"
      - "{{ wgcloud_deploy_path }}/entrypoint.sh:/entrypoint.sh"
    environment:
      LC_ALL: "zh_cn.UTF-8"
      HASH_ENTRYPOINT: "{{ lookup('file', 'wgcloud-docker-entrypoint.sh') | striptags | trim | hash('md5') }}"
      HASH_APPLICATION: "{{ combined_application | striptags | trim | hash('md5') }}"
    entrypoint: "/entrypoint.sh"

{% if 'wgcloud' in group_names %}
  server:
    image: "{{ wgcloud_docker_imgs.base }}"
    container_name: "wgcloud-server"
    restart: "always"
    volumes:
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime"
      - "{{ wgcloud_deploy_path }}/server:/opt/server"
      - "{{ wgcloud_deploy_path }}/entrypoint.sh:/entrypoint.sh"
      - "{{ wgcloud_deploy_path }}/server-application.yml:/opt/server/config/application.yml:ro"
    environment:
      LC_ALL: "zh_cn.UTF-8"
      HASH_ENTRYPOINT: "{{ lookup('file', 'wgcloud-docker-entrypoint.sh') | striptags | trim | hash('md5') }}"
      HASH_APPLICATION: "{{ combined_application | striptags | trim | hash('md5') }}"
    entrypoint: "/entrypoint.sh"
    ports:
      - "{{ wgcloud_port.web }}:9999"
      - "{{ wgcloud_port.ssh }}:9998"
      - "{{ wgcloud_port.daemon }}:9997"
    depends_on:
{% if wgcloud_db_type == 'MySQL' %}
      - mysql
{% endif %}

{% if wgcloud_db_type == 'MySQL' %}
  mysqlVolumeSetup:
    image: "{{ wgcloud_docker_imgs.mysql }}"
    volumes:
      - "{{ wgcloud_deploy_path }}/wgcloud-data:/var/lib/mysql"
    user: "root"
    command: "chown -R mysql:mysql /var/lib/mysql"

  mysql:
    image: "{{ wgcloud_docker_imgs.mysql }}"
    container_name: "{{ wgcloud_db_list.MySQL.hostname }}"
    restart: always
    volumes:
      - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime"
      - "{{ wgcloud_deploy_path }}/wgcloud-MySQL.sql:/docker-entrypoint-initdb.d/wgcloud.sql"
      - "{{ wgcloud_deploy_path }}/wgcloud-data:/var/lib/mysql"
    environment:
      MYSQL_DATABASE: "{{ wgcloud_db_list.MySQL.dbname }}"
      MYSQL_USER: "{{ wgcloud_db_list.MySQL.username }}"
      MYSQL_PASSWORD: "{{ wgcloud_db_list.MySQL.password }}"
      MYSQL_RANDOM_ROOT_PASSWORD: "yes"
    depends_on:
      - mysqlVolumeSetup
    healthcheck:
      test: "mysqladmin --user='$$MYSQL_USER' --password='$$MYSQL_PASSWORD' ping"
      interval: 10s
      timeout: 2s
      retries: 30
{% endif %}
{% endif %}
